﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using QuanLyThuVien.DAL;
using QuanLyThuVien.VO;

namespace QuanLyThuVien.BUS
{
    class busNguoiDung
    {
        daoNguoiDung daoNguoiDung = new daoNguoiDung();

        public List<NguoiDung> ChonDanhSachNguoiDung(String NguoiDung)
        {
            List<NguoiDung> ListNguoiDung = new List<NguoiDung>();
            DataTable dt = daoNguoiDung.ChonDanhSachNguoiDung(NguoiDung);
            foreach (DataRow dr in dt.Rows)
            {
                ListNguoiDung.Add(
                    new NguoiDung(
                                dr["NguoiDung"].ToString()
                                , dr["MatKhau"].ToString()
                                , bool.Parse(dr["QuyenQuanLyNguoiDung"].ToString())
                                , bool.Parse(dr["QuyenQuanLyDocGia"].ToString())
                                , bool.Parse(dr["QuyenQuanLySach"].ToString())
                                , bool.Parse(dr["QuyenQuanLyThongTinMuonTra"].ToString())
                            )
                );
            }
            return ListNguoiDung;
        }

        public NguoiDung ChonNguoiDung(String NguoiDung)
        {
            DataTable dt = daoNguoiDung.ChonNguoiDung(NguoiDung);
            if (dt == null || dt.Rows.Count == 0)
            {
                return null;
            }
            DataRow dr = dt.Rows[0];
            object[] Object = dr.ItemArray;
            if (Object[2].ToString() == "NULL") Object[2] = false;
            if (Object[3].ToString() == "NULL") Object[3] = false;
            if (Object[4].ToString() == "NULL") Object[4] = false;
            if (Object[5].ToString() == "NULL") Object[5] = false;
            return new NguoiDung(Object[0].ToString(),
                                 Object[1].ToString(),
                                 bool.Parse(Object[2].ToString()),
                                 bool.Parse(Object[3].ToString()),
                                 bool.Parse(Object[4].ToString()),
                                 bool.Parse(Object[5].ToString())
                                 );
        }

        public bool DangKy(String NguoiDung, String MatKhau)
        {
            return daoNguoiDung.DangKy(NguoiDung, MatKhau);
        }


        public bool ThayMatKhau(String NguoiDung, String MatKhau)
        {
            return daoNguoiDung.ThayMatKhau(NguoiDung, MatKhau);
        }

        public bool XoaNguoiDung(String NguoiDung)
        {
            return daoNguoiDung.XoaNguoiDung(NguoiDung);
        }

        public DataSet DanhSachNguoiDung(String NguoiDung)
        {
            return daoNguoiDung.DanhSachNguoiDung(NguoiDung);
        }

        public void CapNhatNguoiDung()
        {
            daoNguoiDung.CapNhatNguoiDung();
        }
    }
}
